<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .sale-analysis-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .sale-analysis-btn-export {
    margin-bottom: 10px;
    /* margin-top: -46px; */
    /* float: right; */
  }
  .sale-tag {
    width: 180px;
    height: 50px;
    font-size: 13px;
    text-align: center;
    line-height: 50px;
    margin-left: 3px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>销售分析</span>
    </div>
    <div class="line"></div>
    <sale-analysis-form
      class="sale-analysis-form"
      @on-success-valid="handleSubmit">
    </sale-analysis-form>
    <btn-export :tableData="saleAnalysisList" :tableTitle="columns" :title="title" class="sale-analysis-btn-export"></btn-export>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import SaleAnalysisForm from '_c/sale-analysis-form'
import BtnExport from '_c/btn-export'
import TablePage from '_c/table-page'
export default {
  components: {
    SaleAnalysisForm,
    TablePage,
    BtnExport
  },
  data () {
    return {
      page: 1,
      title: '销售管理销售分析',
      rows: 20,
      confirmData: {},
      postData: {},
      playload: null,
      loading: true,
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 100,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        // {
        //   title: '日期',
        //   key: 'ctime',
        //   minWidth: 150,
        //   align: 'center'
        // },
        {
          title: '推荐销售',
          key: 'uname',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '有效客户数量',
          key: 'cstnum',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '流失客户数量',
          key: 'lostnum',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '总客户量',
          key: 'allnum',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '有效率',
          key: 'yxrate',
          minWidth: 100,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.yxrate ? `${params.row.yxrate}%` : '-')
          }
        },
        {
          title: '流失率',
          key: 'lostrate',
          minWidth: 100,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.lostrate ? `${params.row.lostrate}%` : '-')
          }
        },
        {
          title: '成交平均谈单时间',
          key: 'avgtime',
          minWidth: 135,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.avgtime ? params.row.avgtime : '-')
          }
        },
        {
          title: '销售金额',
          key: 'salemoney',
          minWidth: 150,
          align: 'center'
        }
        // {
        //   title: '操作',
        //   key: 'cz',
        //   // width: 200,
        //   align: 'center',
        //   render: (h, params) => {
        //     return h('div', [
        //       h('Button', {
        //         props: {
        //           type: 'primary',
        //           size: 'small',
        //           ghost: 'ghost'
        //         },
        //         style: {
        //           marginRight: '5px'
        //         },
        //         on: {
        //           click: () => {
        //             this.handleClick(params.row.id, params.row)
        //           }
        //         }
        //       }, '详情')
        //     ])
        //   }
        // }
      ]
    }
  },
  computed: {
    ...mapState({
      saleAnalysisList: state => state.saleAnalysis.saleAnalysisList,
      dataCount: state => state.saleAnalysis.saleAnalysisDataCount,
      historyData: state => state.saleAnalysis.saleAnalysisHistoryData,
      pageSize: state => state.saleAnalysis.saleAnalysisPageSize,
      row: state => state.saleAnalysis.row
    })
  },
  methods: {
    ...mapActions([
      'getSaleAnalysisList',
      'getSaleAnalysisCurrentRow',
      'editSaleAnalysisData',
      'changeSaleAnalysisPage',
      'changeSaleAnalysisPageSize'
    ]),
    handleSubmit (playload) {
      this.loading = true
      // console.log(playload)
      this.playload = playload
      this.getSaleAnalysisList({
        ...playload,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    // handleClick (id, row) {
    //   let newRow = {}
    //   this.id = id
    //   if (row) {
    //     newRow = Object.assign({}, row)
    //   }
    //   this.getSaleDrawerList({ uid: row.id ? row.id : -1 })
    //   this.getSaleCurrentRow(newRow)
    //   this.updateSaleDrawerState(true)
    // },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeSaleAnalysisPage(index)
      this.getSaleAnalysisList({
        page: index,
        rows: this.rows,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeSaleAnalysisPageSize(value)
      this.changePage(this.page)
      this.getSaleAnalysisList({
        rows: value,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getSaleAnalysisList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
